﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate.Burrow;
using FluentNHibernate.Cfg.Db;
using NHibernate.Burrow.Configuration;
//using System.Configuration;
using FluentNHibernate.Conventions.Helpers;
using NHibernate.Cfg;
using NHTools.Tools;

namespace NHTools
{
    public class FluentConfigurator : IConfigurator
    {
        public static NHibernate.Cfg.Configuration NHConfiguration;

        #region IConfigurator Members


        public static void SetConfig()
        {
            var configuration = new Configuration();
            //configuration.Configure();
            configuration.SetProperty(NHibernate.Cfg.Environment.ProxyFactoryFactoryClass, "NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle");

            NHConfiguration = FluentNHibernate.Cfg.Fluently.Configure(configuration)
                               .Database(MsSqlConfiguration.MsSql2005
                                             .ConnectionString(
                                             c => c.Is(System.Configuration.ConfigurationManager.AppSettings["ConStr"])))
                               .Mappings(m => m.FluentMappings.AddFromAssemblyOf<Provider>()
                                   .Conventions.AddFromAssemblyOf<EnumConvention>()
                               .Conventions.Add(ForeignKey.EndsWith("Id")))//.ExportTo(@"E:\kaProjeTfs\AdimAdimDavaKaydi.root\AdimAdimDavaKaydi-NHibernate-Branch\Test\bin\Debug\Maps"))
                               .BuildConfiguration()
                               .SetInterceptor(new NHInterceptor());
        }
        void IConfigurator.Config(IPersistenceUnitCfg puCfg, NHibernate.Cfg.Configuration nhCfg)
        {
            nhCfg.SetProperty(NHibernate.Cfg.Environment.ProxyFactoryFactoryClass, "NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle");

            NHConfiguration = FluentNHibernate.Cfg.Fluently.Configure(nhCfg)
                               .Database(MsSqlConfiguration.MsSql2005
                                             .ConnectionString(
                                             c => c.Is(System.Configuration.ConfigurationManager.AppSettings["ConStr"])))
                               .Mappings(m => m.FluentMappings.AddFromAssemblyOf<Provider>()
                                   .Conventions.AddFromAssemblyOf<EnumConvention>()
                               .Conventions.Add(ForeignKey.EndsWith("Id")))//.ExportTo(@"E:\kaProjeTfs\AdimAdimDavaKaydi.root\AdimAdimDavaKaydi-NHibernate-Branch\Test\bin\Debug\Maps"))
                               .BuildConfiguration()
                               .SetInterceptor(new NHInterceptor());

        }

        void IConfigurator.Config(IBurrowConfig val)
        {
            val.PersistenceUnitCfgs.Add(new PersistenceUnitElement
            {
                Name = "PersistenceUnit1",
                NHConfigFile = null
            });
        }

        #endregion
    }
}
